﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLACQUY.BUS;
using System.Collections;
using QLACQUY.DTO;

namespace QLACQUY
{
    public partial class QuanLySanPham : Form
    {
        public QuanLySanPham()
        {
            InitializeComponent();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            ThemSanPham frm = new ThemSanPham();
            frm.ShowDialog();


        }
        private void fetchDataToGrid()
        {
            dataGridView1.Rows.Clear();
            SanPhamBUS bus = new SanPhamBUS();
            ArrayList arr = bus.layDanhSach();
            for (int i = 0; i < arr.Count; i++ )
            {
                int k = dataGridView1.Rows.Add();
                if (k == 0)
                    dataGridView1.Rows[k].Cells["STT"].Value = 1;
                else
                    dataGridView1.Rows[k].Cells["STT"].Value = int.Parse(dataGridView1.Rows[k - 1].Cells["STT"].Value.ToString()) + 1;
                dataGridView1.Rows[k].Cells["MaSP"].Value = ((SanPhamCRT)arr[i]).MaSanPham;
                dataGridView1.Rows[k].Cells["QuyCach"].Value = ((SanPhamCRT)arr[i]).QuyCach;
                dataGridView1.Rows[k].Cells["NSX"].Value = ((SanPhamCRT)arr[i]).NSX;
                dataGridView1.Rows[k].Cells["GiaSi"].Value = ((SanPhamCRT)arr[i]).DonGiaSy;
                dataGridView1.Rows[k].Cells["GiaLe"].Value = ((SanPhamCRT)arr[i]).DonGiaLe;
                dataGridView1.Rows[k].Cells["MoTa"].Value = ((SanPhamCRT)arr[i]).MoTa;


            }
        }
            

        private void QuanLySanPham_Load(object sender, EventArgs e)
        {
            toolTip1.SetToolTip(btnRefresh, "Nạp lại danh sách sản phẩm");
            fetchDataToGrid();

        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            fetchDataToGrid();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count <= 0)
                return;
            if (MessageBox.Show("Chắc chắn xóa?", "Xac Nhan", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                
                try
                {
                    SanPhamBUS bus = new SanPhamBUS();
                    for (int i = 0; i < dataGridView1.SelectedRows.Count; i++ )
                    {
                    //int index = dataGridView1.SelectedRows[i].
                    int masp = (int)dataGridView1.SelectedRows[i].Cells["MaSP"].Value;
                    bus.xoaSanPham(masp);
                    
                    }
                    MessageBox.Show("Xóa thành công!");
                    fetchDataToGrid();
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Không có dòng nào được chọn!");
                }
                
            }
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count <= 0)
                return;
            ThemSanPham frm = new ThemSanPham();
            frm.IsUpdate = true;
            frm.MaSP = (int)dataGridView1.SelectedRows[0].Cells["MaSP"].Value;
            frm.ShowDialog();
            fetchDataToGrid();
        }
    }
}
